package test.algorithm.astar;

import java.util.List;
import java.util.Map;

public class NumbersParanoiaMain2 {
   
	public static RectGraph rg;
	
	private static int width = 6;
	private static int height = 6;
	
	private static int[][] map;
	private static Map<Integer, Map<String, Integer>> pathToFind;
	
	public static void main(String[] args) {
		
		rg = new RectGraph(true, width, height);
//		map = rg.initGraph();
		
		rg.placeObstacle(3, 2);
		rg.placeObstacle(2, 3);
		
//		rg.print();
		System.out.println("------------------------------");
		
		PathNode start;
		PathNode finish;
		start = rg.getGridNode(2, 2);
		finish = rg.getGridNode(1, 4);
		
		List<PathNode> path = move(start, finish);
		
		for (int i=0; i<path.size()-1; i++) {
			GridNode gridNode = (GridNode) path.get(i);
			rg.placeObstacle(gridNode.getRow(), gridNode.getCol());
		}
		
//		rg.print();
		System.out.println("------------------------------");
		
		start = rg.getGridNode(1, 4);
		finish = rg.getGridNode(3, 3);
		
		path = move(start, finish);
		for (int i=0; i<path.size()-1; i++) {
			GridNode gridNode = (GridNode) path.get(i);
			rg.placeObstacle(gridNode.getRow(), gridNode.getCol());
		}
		
		
		System.out.println("------------------------------");
		
		start = rg.getGridNode(3, 3);
		finish = rg.getGridNode(4, 1);
		
		path = move(start, finish);
		for (int i=0; i<path.size()-1; i++) {
			GridNode gridNode = (GridNode) path.get(i);
			rg.placeObstacle(gridNode.getRow(), gridNode.getCol());
		}
		
		
		System.out.println("------------------------------");
		
		start = rg.getGridNode(4, 1);
		finish = rg.getGridNode(5, 3);
		
		path = move(start, finish);
		for (int i=0; i<path.size()-1; i++) {
			GridNode gridNode = (GridNode) path.get(i);
			rg.placeObstacle(gridNode.getRow(), gridNode.getCol());
		}
		
		
		System.out.println("------------------------------");
		
		start = rg.getGridNode(5, 3);
		finish = rg.getGridNode(4, 5);
		
		path = move(start, finish);
		for (int i=0; i<path.size()-1; i++) {
			GridNode gridNode = (GridNode) path.get(i);
			rg.placeObstacle(gridNode.getRow(), gridNode.getCol());
		}
	}
	
	public static List<PathNode> move(PathNode start, PathNode finish) {
		try {
			
			List<PathNode> path = rg.findBestPath(start, finish);
			rg.print(path);
			
			/*for (int i=1; i<path.size(); i++) {
				GridNode gridNode = (GridNode) path.get(i);
				System.out.print(map[gridNode.getRow()][gridNode.getCol()] + "-->");
			}*/
			
			return path;
			
		} catch (PathNotFoundException e) {
			System.out.println("No path to target. Closest path:");
			rg.print(e.getClosestPath());
		}
		
		return null;
	}
}
